clc
clear
%%
%Delete corresponding annotation symbol, select dataset.
%This one delete Update model in order to increase efficency.
%The default is Vowel dataset.
%DrawGrid(Grids,k) can draw cells in 2-d space, k is figure number.
%10-cross, all time set as 0, affinity voting.
%Data:n-dimension and m types
%data:[d1,d2,...,dn,t,label]
%Grids:[d1,d2,...,dn,p,q1,t1,q2,t2,...,qm,tm]
%di:dimension
%q:types
%t:last active time

%Load data.
load Balance.mat
%load diabetes.mat
%load Haberman.mat
%load Heart-statlog.mat
%load Ionosphere.mat
%load iris.mat
%load kr-vs-kp.mat
%load Letter.mat
%load Lymphography.mat
%load monk1.mat
%load monk2.mat
%load monk3.mat
%load nursery.mat
%load segment.mat
%load Sonar.mat
%load Spambase.mat
%load Vehicle.mat
%load Vowel.mat
%load waveform.mat
%load wine.mat

mingen = 2; 
sym TrainSets;
sym TestSets;
sym CellSets;
sym CeSaSets;

%Balance--1
D = normat([B;L;R]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:49,:);
D2 = D(50:337,:);
D3 = D(338:625,:);
l01 = size(D1,1);
l02 = size(D2,1);
l03 = size(D3,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D3 = [D3';3.*ones(1,l03)]';                                                 
t = 3;

%{
%diabetes--2
D = normat([O;Z]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:268,:);
D2 = D(269:768,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';
t = 2;
%}
%{
%Haberman--3
D = norma([one;two]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:225,:);
D2 = D(226:306,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
t = 2;
%}
%{
%Heart-statlog--4
DD = norma([One;Two]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:150,:);
D02 = DD(151:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
D01 = [D01';ones(1,l01)]';
D02 = [D02';2.*ones(1,l02)]';
t = 2;
%}
%{
%Ionosphere--5
DD = norma([B;G]);
DD(:,2) = [];
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:126,:);
D02 = DD(127:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
D01 = [D01';ones(1,l01)]';
D02 = [D02';2.*ones(1,l02)]';
t = 2;
%}
%{
%iris--6
D = norma([Se;Ve;Vi]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:50,:);
D2 = D(51:100,:);
D3 = D(101:150,:);
l01 = size(D1,1);
l02 = size(D2,1);
l03 = size(D3,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D3 = [D3';3.*ones(1,l03)]';                                                
t = 3;
%}
%{
%kr-vs-kp--7
[coeff,score,latent] = pca([win;nowin]);
DD = norma(score);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:1669,:);
D02 = DD(1670:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
D01 = [D01';ones(1,l01)]';
D02 = [D02';2.*ones(1,l02)]';
t = 2;
%}
%{
%Letter--8
DD = normat([A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:789,:);
D02 = DD(790:1555,:);
D03 = DD(1556:2291,:);
D04 = DD(2292:3096,:);
D05 = DD(3097:3864,:);
D06 = DD(3865:4639,:);
D07 = DD(4640:5412,:);
D08 = DD(5413:6146,:);
D09 = DD(6147:6901,:);
D10 = DD(6902:7648,:);
D11 = DD(7649:8387,:);
D12 = DD(8388:9148,:);
D13 = DD(9149:9940,:);
D14 = DD(9941:10723,:);
D15 = DD(10724:11476,:);
D16 = DD(11477:12279,:);
D17 = DD(12280:13062,:);
D18 = DD(13063:13820,:);
D19 = DD(13821:14568,:);
D20 = DD(14569:15364,:);
D21 = DD(15365:16177,:);
D22 = DD(16178:16941,:);
D23 = DD(16942:17693,:);
D24 = DD(17694:18480,:);
D25 = DD(18481:19266,:);
D26 = DD(19267:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
l04 = size(D04,1);
l05 = size(D05,1);
l06 = size(D06,1);
l07 = size(D07,1);
l08 = size(D08,1);
l09 = size(D09,1);
l10 = size(D10,1);
l11 = size(D11,1);
l12 = size(D12,1);
l13 = size(D13,1);
l14 = size(D14,1);
l15 = size(D15,1);
l16 = size(D16,1);
l17 = size(D17,1);
l18 = size(D18,1);
l19 = size(D19,1);
l20 = size(D20,1);
l21 = size(D21,1);
l22 = size(D22,1);
l23 = size(D23,1);
l24 = size(D24,1);
l25 = size(D25,1);
l26 = size(D26,1);
D01 = [D01';ones(1,l01)]';                                                 
D02 = [D02';2.*ones(1,l02)]';                             
D03 = [D03';3.*ones(1,l03)]';                       
D04 = [D04';4.*ones(1,l04)]';
D05 = [D05';5.*ones(1,l05)]';
D06 = [D06';6.*ones(1,l06)]';
D07 = [D07';7.*ones(1,l07)]';
D08 = [D08';8.*ones(1,l08)]';
D09 = [D09';9.*ones(1,l09)]';
D10 = [D10';10.*ones(1,l10)]';
D11 = [D11';11.*ones(1,l11)]';
D12 = [D12';12.*ones(1,l12)]';
D13 = [D13';13.*ones(1,l13)]';
D14 = [D14';14.*ones(1,l14)]';
D15 = [D15';15.*ones(1,l15)]';
D16 = [D16';16.*ones(1,l16)]';
D17 = [D17';17.*ones(1,l17)]';
D18 = [D18';18.*ones(1,l18)]';
D19 = [D19';19.*ones(1,l19)]';
D20 = [D20';20.*ones(1,l20)]';
D21 = [D21';21.*ones(1,l21)]';
D22 = [D22';22.*ones(1,l22)]';
D23 = [D23';23.*ones(1,l23)]';
D24 = [D24';24.*ones(1,l24)]';
D25 = [D25';25.*ones(1,l25)]';
D26 = [D26';26.*ones(1,l26)]';
t = 26;
%}
%{
%Lymphography--9
DD = normat([A;B;C;D]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:2,:);
D02 = DD(3:83,:);
D03 = DD(84:144,:);
D04 = DD(145:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
l04 = size(D04,1);
D01 = [D01';ones(1,l01)]';                                                    %The lable of 1st class samples
D02 = [D02';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D03 = [D03';3.*ones(1,l03)]';                                              
D04 = [D04';4.*ones(1,l04)]';
t = 4;
%}
%{
%monk1--10
D = normat([Tr1;Tr2]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:62,:);
D2 = D(63:124,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';
D2 = [D2';2.*ones(1,l02)]';
l1 = size(D1,1);
l2 = size(D2,1);
t = 2;
%}
%{
%monk2--11 
D = normat([Tr1;Tr2]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:105,:);
D2 = D(106:169,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';
D2 = [D2';2.*ones(1,l02)]';
l1 = size(D1,1);
l2 = size(D2,1);
t = 2;
%}
%{
%monk3--12
D = normat([Tr1;Tr2]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:62,:);
D2 = D(63:122,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';
D2 = [D2';2.*ones(1,l02)]';
l1 = size(D1,1);
l2 = size(D2,1);
t = 2;
%}
%{
%nursery--13
D = normat([NR;P;R;SP;VR]);
n = size(D,1);
D = [D';zeros(1,n)]';
D01 = D(1:4320,:);
D02 = D(4321:8586,:);
D03 = D(8587:8588,:);
D04 = D(8589:12632,:);
D05 = D(12633:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
l04 = size(D04,1);
l05 = size(D05,1);
D01 = [D01';ones(1,l01)]';                                                    %The lable of 1st class samples
D02 = [D02';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D03 = [D03';3.*ones(1,l03)]';                                             
D04 = [D04';4.*ones(1,l04)]';
D05 = [D05';5.*ones(1,l05)]';
t = 5;
%}
%{
%segment--14
D = [Br;Ce;Fo;Gr;Pa;Sk;Wi];
D(:,3) = [];
D = norma(D);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:330,:);
D2 = D(331:660,:);
D3 = D(661:990,:);
D4 = D(991:1320,:);
D5 = D(1321:1650,:);
D6 = D(1651:1980,:);
D7 = D(1981:2310,:);
l01 = size(D1,1);
l02 = size(D2,1);
l03 = size(D3,1);
l04 = size(D4,1);
l05 = size(D5,1);
l06 = size(D6,1);
l07 = size(D7,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D3 = [D3';3.*ones(1,l03)]';                                                 %The lable of 1st class samples
D4 = [D4';4.*ones(1,l04)]';
D5 = [D5';5.*ones(1,l05)]';
D6 = [D6';6.*ones(1,l06)]';
D7 = [D7';7.*ones(1,l07)]';
t = 7;
%}
%{
%Sonar--15
D = norma([R;M]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:97,:);
D2 = D(98:end,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
t = 2;
%}
%{
%Spambase--16
[coeff,score,latent] = pca([One;Zero]);
D = normat(score);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:1813,:);
D2 = D(1814:end,:);
l01 = size(D1,1);
l02 = size(D2,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
t = 2;
%}
%{
%Vehicle--17
DD = normat([bus;opel;saab;van]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:218,:);
D02 = DD(219:430,:);
D03 = DD(431:647,:);
D04 = DD(648:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
l04 = size(D04,1);
D01 = [D01';ones(1,l01)]';                                                    %The lable of 1st class samples
D02 = [D02';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D03 = [D03';3.*ones(1,l03)]';                                                 %The lable of 1st class samples
D04 = [D04';4.*ones(1,l04)]';
t = 4;
%}
%{
%Vowel--18
DD = normat([A;B;C;D;E;F;G;H;I;J;K]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:90,:);
D02 = DD(91:180,:);
D03 = DD(181:270,:);
D04 = DD(271:360,:);
D05 = DD(361:450,:);
D06 = DD(451:540,:);
D07 = DD(541:630,:);
D08 = DD(631:720,:);
D09 = DD(721:810,:);
D10 = DD(811:900,:);
D11 = DD(901:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
l04 = size(D04,1);
l05 = size(D05,1);
l06 = size(D06,1);
l07 = size(D07,1);
l08 = size(D08,1);
l09 = size(D09,1);
l10 = size(D10,1);
l11 = size(D11,1);
D01 = [D01';ones(1,l01)]';                                                    %The lable of 1st class samples
D02 = [D02';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D03 = [D03';3.*ones(1,l03)]';                                             
D04 = [D04';4.*ones(1,l04)]';
D05 = [D05';5.*ones(1,l05)]';
D06 = [D06';6.*ones(1,l06)]';
D07 = [D07';7.*ones(1,l07)]';
D08 = [D08';8.*ones(1,l08)]';
D09 = [D09';9.*ones(1,l09)]';
D10 = [D10';10.*ones(1,l10)]';
D11 = [D11';11.*ones(1,l11)]';
t = 11;
%}
%{
%waveform--19
DD = normat([A;B;C]);
n = size(DD,1);
DD = [DD';zeros(1,n)]';
D01 = DD(1:1692,:);
D02 = DD(1693:3345,:);
D03 = DD(3346:end,:);
l01 = size(D01,1);
l02 = size(D02,1);
l03 = size(D03,1);
D01 = [D01';ones(1,l01)]';                                                    %The lable of 1st class samples
D02 = [D02';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D03 = [D03';3.*ones(1,l03)]';                                              
t = 3;
%}
%{
%wine--20 
D = normat([One;Two;Thr]);
n = size(D,1);
D = [D';zeros(1,n)]';
D1 = D(1:59,:);
D2 = D(60:131,:);
D3 = D(132:end,:);
l01 = size(D1,1);
l02 = size(D2,1);
l03 = size(D3,1);
D1 = [D1';ones(1,l01)]';                                                    %The lable of 1st class samples
D2 = [D2';2.*ones(1,l02)]';                                                 %The lable of 2ed class samples
D3 = [D3';3.*ones(1,l03)]';                                                 
t = 3;
%}

for j = 1 : 10

    JG = [];
    
    t01 = randperm(l01);
    t02 = randperm(l02);
    t03 = randperm(l03);
    %t04 = randperm(l04);
    %t05 = randperm(l05);
    %t06 = randperm(l06);
    %t07 = randperm(l07);
    %t08 = randperm(l08);
    %t09 = randperm(l09);
    %t10 = randperm(l10);
    %t11 = randperm(l11);
    %t12 = randperm(l12);
    %t13 = randperm(l13);
    %t14 = randperm(l14);
    %t15 = randperm(l15);
    %t16 = randperm(l16);
    %t17 = randperm(l17);
    %t18 = randperm(l18);
    %t19 = randperm(l19);   
    %t20 = randperm(l20);
    %t21 = randperm(l21);
    %t22 = randperm(l22);
    %t23 = randperm(l23);
    %t24 = randperm(l24);
    %t25 = randperm(l25);
    %t26 = randperm(l26);

    for i = 1 : 10  
        
        %Balance--1
        DD1 = D1;
        DD2 = D2;
        DD3 = D3;
        if i < 10
            Te1 = DD1(t01((i-1)*5+1:i*5),:);
            DD1(t01((i-1)*5+1:i*5),:) = [];
        else
            Te1 = DD1(t01(46:end),:);
            DD1(t01(46:end),:) = [];
        end        
        if i <= 2
            Te2 = DD2(t02((i-1)*28+1:i*28),:);
            Te3 = DD3(t03((i-1)*28+1:i*28),:);
            DD2(t02((i-1)*28+1:i*28),:) = [];
            DD3(t03((i-1)*28+1:i*28),:) = [];
        end
        if i > 2 && i < 10
            Te2 = DD2(t02(56+(i-3)*29+1:56+(i-2)*29),:);
            Te3 = DD3(t03(56+(i-3)*29+1:56+(i-2)*29),:);
            DD2(t02(56+(i-3)*29+1:56+(i-2)*29),:) = [];
            DD3(t03(56+(i-3)*29+1:56+(i-2)*29),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(260:end),:);
            Te3 = DD3(t03(260:end),:);
            DD2(t02(260:end),:) = [];
            DD3(t03(260:end),:) = [];
        end
        Tr1 = DD1;
        Tr2 = DD2;
        Tr3 = DD3;
        TrainData = [Tr1;Tr2;Tr3];
        TestData = [Te1;Te2;Te3];       
                  
        %{
        %diabetes--2
        DD1 = D1;
        DD2 = D2;
        if i <= 2
            Te1 = DD1(t01((i-1)*26+1:i*26),:);
            DD1(t01((i-1)*26+1:i*26),:) = [];
        end
        if i > 2 && i < 10
            Te1 = DD1(t01(52+(i-3)*27+1:52+(i-2)*27),:);
            DD1(t01(52+(i-3)*27+1:52+(i-2)*27),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(242:end),:);
            DD1(t01(242:end),:) = [];
        end      
        Te2 = DD2(t02((i-1)*50+1:i*50),:);
        DD2(t02((i-1)*50+1:i*50),:) = [];
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];              
        %}
        %{
        %Haberman--3
        DD1 = D1;
        DD2 = D2;
        if i <= 5
            Te1 = DD1(t01((i-1)*22+1:i*22),:);
            DD1(t01((i-1)*22+1:i*22),:) = [];
        end
        if i > 5 && i < 10
            Te1 = DD1(t01(110+(i-6)*23+1:110+(i-5)*23),:);
            DD1(t01(110+(i-6)*23+1:110+(i-5)*23),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(203:end),:);
            DD1(t01(203:end),:) = [];
        end
        if i < 10
            Te2 = DD2(t02((i-1)*8+1:i*8),:);
            DD2(t02((i-1)*8+1:i*8),:) = [];
        else
            Te2 = DD2(t02(73:end),:);
            DD2(t02(73:end),:) = [];
        end
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];        
        %}
        %{
        %Heart-statlog--4
        DD1 = D01;
        DD2 = D02;
        Te1 = DD1(t01((i-1)*15+1:i*15),:);
        Te2 = DD2(t02((i-1)*12+1:i*12),:);
        DD1(t01((i-1)*15+1:i*15),:) = [];
        DD2(t02((i-1)*12+1:i*12),:) = [];
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];        
        %}
        %{
        %Ionosphere--5
        DD1 = D01;
        DD2 = D02;
        if i <= 6
            Te1 = DD1(t01((i-1)*13+1:i*13),:);
            DD1(t01((i-1)*13+1:i*13),:) = [];
        end
        if i > 6 && i < 10
            Te1 = DD1(t01(78+(i-7)*12+1:78+(i-6)*12),:);
            DD1(t01(78+(i-7)*12+1:78+(i-6)*12),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(115:end),:);
            DD1(t01(115:end),:) = [];
        end               
        if i <= 5
            Te2 = DD2(t02((i-1)*23+1:i*23),:);
            DD2(t02((i-1)*23+1:i*23),:) = [];
        end
        if i > 5 && i < 10
            Te2 = DD2(t02(115+(i-6)*22+1:115+(i-5)*22),:);
            DD2(t02(115+(i-6)*22+1:115+(i-5)*22),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(204:end),:);
            DD2(t02(204:end),:) = [];
        end         
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];        
        %}
        %{
        %Iris--6
        DD1 = D1;
        DD2 = D2;
        DD3 = D3;
        Te1 = DD1(t01((i-1)*5+1:i*5),:);
        Te2 = DD2(t02((i-1)*5+1:i*5),:);
        Te3 = DD3(t03((i-1)*5+1:i*5),:);
        DD1(t01((i-1)*5+1:i*5),:) = [];
        DD2(t02((i-1)*5+1:i*5),:) = [];
        DD3(t03((i-1)*5+1:i*5),:) = [];
        Tr1 = DD1;
        Tr2 = DD2;
        Tr3 = DD3;
        TrainData = [Tr1;Tr2;Tr3];
        TestData = [Te1;Te2;Te3];        
        %}
        %{
        %kr-vs-kp--7
        DD1 = D01;
        DD2 = D02;        
        if i < 10
            Te1 = DD1(t01((i-1)*167+1:i*167),:);
            DD1(t01((i-1)*167+1:i*167),:) = [];
        else
            Te1 = DD1(t01(1504:end),:);
            DD1(t01(1504:end),:) = [];
        end 
        if i <= 3
            Te2 = DD2(t02((i-1)*152+1:i*152),:);
            DD2(t02((i-1)*152+1:i*152),:) = [];
        end
        if i > 3 && i < 10
            Te2 = DD2(t02(456+(i-4)*153+1:456+(i-3)*153),:);
            DD2(t02(456+(i-4)*153+1:456+(i-3)*153),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(1375:end),:);
            DD2(t02(1375:end),:) = [];
        end             
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];         
        %}
        %{
        %Letter--8
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        DD04 = D04;
        DD05 = D05;
        DD06 = D06;
        DD07 = D07;
        DD08 = D08;
        DD09 = D09;
        DD10 = D10;
        DD11 = D11;
        DD12 = D12;
        DD13 = D13;
        DD14 = D14;
        DD15 = D15;
        DD16 = D16;
        DD17 = D17;
        DD18 = D18;
        DD19 = D19;       
        DD20 = D20;
        DD21 = D21;
        DD22 = D22;
        DD23 = D23;
        DD24 = D24;
        DD25 = D25;
        DD26 = D26;
        %1A
        if i < 10
            Te01 = DD01(t01((i-1)*79+1:i*79),:);
            DD01(t01((i-1)*79+1:i*79),:) = [];
        else
            Te01 = DD01(t01(712:end),:);
            DD01(t01(712:end),:) = [];
        end
        %2B
        if i <= 6
            Te02 = DD02(t02((i-1)*77+1:i*77),:);
            DD02(t02((i-1)*77+1:i*77),:) = [];
        end
        if i > 6 && i < 10
            Te02 = DD02(t02(462+(i-7)*76+1:462+(i-6)*76),:);
            DD02(t02(462+(i-7)*76+1:462+(i-6)*76),:) = [];
        end
        if i == 10
            Te02 = DD02(t02(691:end),:);
            DD02(t02(691:end),:) = [];
        end          
        %3C
        if i <= 6
            Te03 = DD03(t03((i-1)*74+1:i*74),:);
            DD03(t03((i-1)*74+1:i*74),:) = [];
        end
        if i > 6 && i < 10
            Te03 = DD03(t03(444+(i-7)*73+1:444+(i-6)*73),:);
            DD03(t03(444+(i-7)*73+1:444+(i-6)*73),:) = [];
        end
        if i == 10
            Te03 = DD03(t03(664:end),:);
            DD03(t03(664:end),:) = [];
        end         
        %4D
        if i <= 5
            Te04 = DD04(t04((i-1)*81+1:i*81),:);
            DD04(t04((i-1)*81+1:i*81),:) = [];
        end
        if i > 5 && i < 10
            Te04 = DD04(t04(405+(i-6)*80+1:405+(i-5)*80),:);
            DD04(t04(405+(i-6)*80+1:405+(i-5)*80),:) = [];
        end
        if i == 10
            Te04 = DD04(t04(726:end),:);
            DD04(t04(726:end),:) = [];
        end          
        %5E
        if i <= 8
            Te05 = DD05(t05((i-1)*77+1:i*77),:);
            DD05(t05((i-1)*77+1:i*77),:) = [];
        end
        if i > 8 && i < 10
            Te05 = DD05(t05(616+(i-9)*76+1:616+(i-8)*76),:);
            DD05(t05(616+(i-9)*76+1:616+(i-8)*76),:) = [];
        end
        if i == 10
            Te05 = DD05(t05(693:end),:);
            DD05(t05(693:end),:) = [];
        end        
        %6F
        if i <= 5
            Te06 = DD06(t06((i-1)*78+1:i*78),:);
            DD06(t06((i-1)*78+1:i*78),:) = [];
        end
        if i > 5 && i < 10
            Te06 = DD06(t06(390+(i-6)*77+1:390+(i-5)*77),:);
            DD06(t06(390+(i-6)*77+1:390+(i-5)*77),:) = [];
        end
        if i == 10
            Te06 = DD06(t06(699:end),:);
            DD06(t06(699:end),:) = [];
        end                  
        %7G
        if i <= 3
            Te07 = DD07(t07((i-1)*78+1:i*78),:);
            DD07(t07((i-1)*78+1:i*78),:) = [];
        end
        if i > 3 && i < 10
            Te07 = DD07(t07(234+(i-4)*77+1:234+(i-3)*77),:);
            DD07(t07(234+(i-4)*77+1:234+(i-3)*77),:) = [];
        end
        if i == 10
            Te07 = DD07(t07(697:end),:);
            DD07(t07(697:end),:) = [];
        end        
        %8H
        if i <= 4
            Te08 = DD08(t08((i-1)*74+1:i*74),:);
            DD08(t08((i-1)*74+1:i*74),:) = [];
        end
        if i > 4 && i < 10
            Te08 = DD08(t08(296+(i-5)*73+1:296+(i-4)*73),:);
            DD08(t08(296+(i-5)*73+1:296+(i-4)*73),:) = [];
        end
        if i == 10
            Te08 = DD08(t08(662:end),:);
            DD08(t08(662:end),:) = [];
        end        
        %9I
        if i <= 5
            Te09 = DD09(t09((i-1)*76+1:i*76),:);
            DD09(t09((i-1)*76+1:i*76),:) = [];
        end
        if i > 5 && i < 10
            Te09 = DD09(t09(380+(i-6)*75+1:380+(i-5)*75),:);
            DD09(t09(380+(i-6)*75+1:380+(i-5)*75),:) = [];
        end
        if i == 10
            Te09 = DD09(t09(681:end),:);
            DD09(t09(681:end),:) = [];
        end                  
        %10J
        if i <= 7
            Te10 = DD10(t10((i-1)*75+1:i*75),:);
            DD10(t10((i-1)*75+1:i*75),:) = [];
        end
        if i > 7 && i < 10
            Te10 = DD10(t10(525+(i-8)*74+1:525+(i-7)*74),:);
            DD10(t10(525+(i-8)*74+1:525+(i-7)*74),:) = [];
        end
        if i == 10
            Te10 = DD10(t10(674:end),:);
            DD10(t10(674:end),:) = [];
        end                  
        %11K
        if i < 10
            Te11 = DD11(t11((i-1)*74+1:i*74),:);
            DD11(t11((i-1)*74+1:i*74),:) = [];
        else
            Te11 = DD11(t11(667:end),:);
            DD11(t11(667:end),:) = [];
        end        
        %12L
        if i < 10
            Te12 = DD12(t12((i-1)*76+1:i*76),:);
            DD12(t12((i-1)*76+1:i*76),:) = [];
        else
            Te12 = DD12(t12(685:end),:);
            DD12(t12(685:end),:) = [];
        end        
        %13M
        if i <= 2
            Te13 = DD13(t13((i-1)*80+1:i*80),:);
            DD13(t13((i-1)*80+1:i*80),:) = [];
        end
        if i > 2 && i < 10
            Te13 = DD13(t13(160+(i-3)*79+1:160+(i-2)*79),:);
            DD13(t13(160+(i-3)*79+1:160+(i-2)*79),:) = [];
        end
        if i == 10
            Te13 = DD13(t13(714:end),:);
            DD13(t13(714:end),:) = [];
        end            
        %14N
        if i <= 3
            Te14 = DD14(t14((i-1)*79+1:i*79),:);
            DD14(t14((i-1)*79+1:i*79),:) = [];
        end
        if i > 3 && i < 10
            Te14 = DD14(t14(237+(i-4)*78+1:237+(i-3)*78),:);
            DD14(t14(237+(i-4)*78+1:237+(i-3)*78),:) = [];
        end
        if i == 10
            Te14 = DD14(t14(706:end),:);
            DD14(t14(706:end),:) = [];
        end         
        %15O     
        if i <= 3
            Te15 = DD15(t15((i-1)*76+1:i*76),:);
            DD15(t15((i-1)*76+1:i*76),:) = [];
        end
        if i > 3 && i < 10
            Te15 = DD15(t15(228+(i-4)*75+1:228+(i-3)*75),:);
            DD15(t15(228+(i-4)*75+1:228+(i-3)*75),:) = [];
        end
        if i == 10
            Te15 = DD15(t15(679:end),:);
            DD15(t15(679:end),:) = [];
        end        
        %16P
        if i <= 3
            Te16 = DD16(t16((i-1)*81+1:i*81),:);
            DD16(t16((i-1)*81+1:i*81),:) = [];
        end
        if i > 3 && i < 10
            Te16 = DD16(t16(243+(i-4)*80+1:243+(i-3)*80),:);
            DD16(t16(243+(i-4)*80+1:243+(i-3)*80),:) = [];
        end
        if i == 10
            Te16 = DD16(t16(724:end),:);
            DD16(t16(724:end),:) = [];
        end         
        %17Q
        if i <= 3
            Te17 = DD17(t17((i-1)*79+1:i*79),:);
            DD17(t17((i-1)*79+1:i*79),:) = [];
        end
        if i > 3 && i < 10
            Te17 = DD17(t17(237+(i-4)*78+1:237+(i-3)*78),:);
            DD17(t17(237+(i-4)*78+1:237+(i-3)*78),:) = [];
        end
        if i == 10
            Te17 = DD17(t17(706:end),:);
            DD17(t17(706:end),:) = [];
        end         
        %18R
        if i <= 8
            Te18 = DD18(t18((i-1)*76+1:i*76),:);
            DD18(t18((i-1)*76+1:i*76),:) = [];
        end
        if i > 8 && i < 10
            Te18 = DD18(t18(608+(i-9)*75+1:608+(i-8)*75),:);
            DD18(t18(608+(i-9)*75+1:608+(i-8)*75),:) = [];
        end
        if i == 10
            Te18 = DD18(t18(684:end),:);
            DD18(t18(684:end),:) = [];
        end                
        %19S       
        if i <= 8
            Te19 = DD19(t19((i-1)*75+1:i*75),:);
            DD19(t19((i-1)*75+1:i*75),:) = [];
        end
        if i > 8 && i < 10
            Te19 = DD19(t19(600+(i-9)*74+1:600+(i-8)*74),:);
            DD19(t19(600+(i-9)*74+1:600+(i-8)*74),:) = [];
        end
        if i == 10
            Te19 = DD19(t19(675:end),:);
            DD19(t19(675:end),:) = [];
        end        
        %20T
        if i <= 6
            Te20 = DD20(t20((i-1)*80+1:i*80),:);
            DD20(t20((i-1)*80+1:i*80),:) = [];
        end
        if i > 6 && i < 10
            Te20 = DD20(t20(480+(i-7)*79+1:480+(i-6)*79),:);
            DD20(t20(480+(i-7)*79+1:480+(i-6)*79),:) = [];
        end
        if i == 10
            Te20 = DD20(t20(718:end),:);
            DD20(t20(718:end),:) = [];
        end           
        %21U
        if i <= 3
            Te21 = DD21(t21((i-1)*82+1:i*82),:);
            DD21(t21((i-1)*82+1:i*82),:) = [];
        end
        if i > 3 && i < 10
            Te21 = DD21(t21(246+(i-4)*81+1:246+(i-3)*81),:);
            DD21(t21(246+(i-4)*81+1:246+(i-3)*81),:) = [];
        end
        if i == 10
            Te21 = DD21(t21(733:end),:);
            DD21(t21(733:end),:) = [];
        end           
        %22V
        if i <= 4
            Te22 = DD22(t22((i-1)*77+1:i*77),:);
            DD22(t22((i-1)*77+1:i*77),:) = [];
        end
        if i > 4 && i < 10
            Te22 = DD22(t22(308+(i-5)*76+1:308+(i-4)*76),:);
            DD22(t22(308+(i-5)*76+1:308+(i-4)*76),:) = [];
        end
        if i == 10
            Te22 = DD22(t22(689:end),:);
            DD22(t22(689:end),:) = [];
        end         
        %23W 
        if i <= 2
            Te23 = DD23(t23((i-1)*76+1:i*76),:);
            DD23(t23((i-1)*76+1:i*76),:) = [];
        end
        if i > 2 && i < 10
            Te23 = DD23(t23(152+(i-3)*75+1:152+(i-2)*75),:);
            DD23(t23(152+(i-3)*75+1:152+(i-2)*75),:) = [];
        end
        if i == 10
            Te23 = DD23(t23(678:end),:);
            DD23(t23(678:end),:) = [];
        end          
        %24X
        if i <= 7
            Te24 = DD24(t24((i-1)*79+1:i*79),:);
            DD24(t24((i-1)*79+1:i*79),:) = [];
        end
        if i > 7 && i < 10
            Te24 = DD24(t24(553+(i-8)*78+1:553+(i-7)*78),:);
            DD24(t24(553+(i-8)*78+1:553+(i-7)*78),:) = [];
        end
        if i == 10
            Te24 = DD24(t24(710:end),:);
            DD24(t24(710:end),:) = [];
        end        
        %25Y
        if i <= 6
            Te25 = DD25(t25((i-1)*79+1:i*79),:);
            DD25(t25((i-1)*79+1:i*79),:) = [];
        end
        if i > 6 && i < 10
            Te25 = DD25(t25(474+(i-7)*78+1:474+(i-6)*78),:);
            DD25(t25(474+(i-7)*78+1:474+(i-6)*78),:) = [];
        end
        if i == 10
            Te25 = DD25(t25(709:end),:);
            DD25(t25(709:end),:) = [];
        end                   
        %26Z
        if i <= 4
            Te26 = DD26(t26((i-1)*74+1:i*74),:);
            DD26(t26((i-1)*74+1:i*74),:) = [];
        end
        if i > 4 && i < 10
            Te26 = DD26(t26(296+(i-5)*73+1:296+(i-4)*73),:);
            DD26(t26(296+(i-5)*73+1:296+(i-4)*73),:) = [];
        end
        if i == 10
            Te26 = DD26(t26(662:end),:);
            DD26(t26(662:end),:) = [];
        end                     
        Tr01 = DD01;
        Tr02 = DD02;
        Tr03 = DD03;
        Tr04 = DD04;
        Tr05 = DD05;
        Tr06 = DD06;
        Tr07 = DD07;
        Tr08 = DD08;
        Tr09 = DD09; 
        Tr10 = DD10;
        Tr11 = DD11;
        Tr12 = DD12;
        Tr13 = DD13;
        Tr14 = DD14;
        Tr15 = DD15;
        Tr16 = DD16;
        Tr17 = DD17;
        Tr18 = DD18;
        Tr19 = DD19; 
        Tr20 = DD20;
        Tr21 = DD21;
        Tr22 = DD22;
        Tr23 = DD23;
        Tr24 = DD24;
        Tr25 = DD25;
        Tr26 = DD26;       
        TrainData = [Tr01;Tr02;Tr03;Tr04;Tr05;Tr06;Tr07;Tr08;Tr09;Tr10;Tr11;Tr12;Tr13;Tr14;Tr15;Tr16;Tr17;Tr18;Tr19;Tr20;Tr21;Tr22;Tr23;Tr24;Tr25;Tr26];
        TestData =  [Te01;Te02;Te03;Te04;Te05;Te06;Te07;Te08;Te09;Te10;Te11;Te12;Te13;Te14;Te15;Te16;Te17;Te18;Te19;Te20;Te21;Te22;Te23;Te24;Te25;Te26];        
        %}
        %{
        %Lymphography--9
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        DD04 = D04;        
        if i < 3
            Te01 = DD01(t01((i-1)*1+1:i*1),:);
            DD01(t01((i-1)*1+1:i*1),:) = [];
        else
            Te01 = [];
        end        
        if i < 10
            Te02 = DD02(t02((i-1)*8+1:i*8),:);
            DD02(t02((i-1)*8+1:i*8),:) = [];
        else
            Te02 = DD02(t02(73:end),:);
            DD02(t02(73:end),:) = [];
        end        
        if i < 10
            Te03 = DD03(t03((i-1)*6+1:i*6),:);
            DD03(t03((i-1)*6+1:i*6),:) = [];
        else
            Te03 = DD03(t03(55:end),:);
            DD03(t03(55:end),:) = [];
        end
        if i > 5 && i < 10
            Te04 = DD04(t04((i-6)*1+1:(i-5)*1),:);
            DD04(t04((i-6)*1+1:(i-5)*1),:) = [];
        else
            Te04 = [];
        end
        Tr01 = DD01;
        Tr02 = DD02;
        Tr03 = DD03;
        Tr04 = DD04;        
        TrainData = [Tr01;Tr02;Tr03;Tr04];
        TestData =  [Te01;Te02;Te03;Te04];             
        %}
        %{
        %monk1--10
        DD1 = D1;
        DD2 = D2;
        if i <= 8
            Te1 = DD1(t01((i-1)*6+1:i*6),:);
            DD1(t01((i-1)*6+1:i*6),:) = [];
            Te2 = DD2(t02((i-1)*6+1:i*6),:);
            DD2(t02((i-1)*6+1:i*6),:) = [];
        end
        if i > 9 && i < 10
            Te1 = DD1(t01(48+(i-9)*7+1:48+(i-8)*7),:);
            DD1(t01(48+(i-9)*7+1:48+(i-8)*7),:) = [];
            Te2 = DD2(t02(48+(i-9)*7+1:48+(i-8)*7),:);
            DD2(t02(48+(i-9)*7+1:48+(i-8)*7),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(56:end),:);
            DD1(t01(56:end),:) = [];
            Te2 = DD2(t02(56:end),:);
            DD2(t02(56:end),:) = [];
        end  
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];          
        %}
        %{
        %monk2--11
        DD1 = D1;
        DD2 = D2;
        if i <= 5
            Te1 = DD1(t01((i-1)*10+1:i*10),:);
            DD1(t01((i-1)*10+1:i*10),:) = [];
        end
        if i > 6 && i < 10
            Te1 = DD1(t01(50+(i-6)*11+1:50+(i-5)*11),:);
            DD1(t01(50+(i-6)*11+1:50+(i-5)*11),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(95:end),:);
            DD1(t01(95:end),:) = [];
        end
        if i <= 6
            Te2 = DD2(t02((i-1)*6+1:i*6),:);
            DD2(t02((i-1)*6+1:i*6),:) = [];
        end
        if i > 7 && i < 10
            Te2 = DD2(t02(36+(i-7)*7+1:36+(i-6)*7),:);
            DD2(t02(36+(i-7)*7+1:36+(i-6)*7),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(58:end),:);
            DD2(t02(58:end),:) = [];
        end
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];        
        %}
        %{
        %monk3--12
        DD1 = D1;
        DD2 = D2;
        if i <= 8
            Te1 = DD1(t01((i-1)*6+1:i*6),:);
            DD1(t01((i-1)*6+1:i*6),:) = [];
        end
        if i > 9 && i < 10
            Te1 = DD1(t01(48+(i-9)*7+1:48+(i-8)*7),:);
            DD1(t01(48+(i-9)*7+1:48+(i-8)*7),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(56:end),:);
            DD1(t01(56:end),:) = [];
        end         
        Te2 = DD2(t02((i-1)*6+1:i*6),:);
        DD2(t02((i-1)*6+1:i*6),:) = [];
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];           
        %}
        %{
        %nursery--13
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        DD04 = D04;
        DD05 = D05;
        Te1 = DD01(t01((i-1)*432+1:i*432),:);
        DD01(t01((i-1)*432+1:i*432),:) = [];      
        if i <= 4
            Te2 = DD02(t02((i-1)*426+1:i*426),:);
            DD02(t02((i-1)*426+1:i*426),:) = [];
            Te4 = DD04(t04((i-1)*405+1:i*405),:);
            DD04(t04((i-1)*405+1:i*405),:) = [];
        end
        if i > 4 && i < 10
            Te2 = DD02(t02(1705+(i-5)*427+1:1705+(i-4)*427),:);
            DD02(t02(1705+(i-5)*427+1:1705+(i-4)*427),:) = [];
            Te4 = DD04(t04(1621+(i-5)*404+1:1621+(i-4)*404),:);
            DD04(t04(1621+(i-5)*404+1:1621+(i-4)*404),:) = [];
        end
        if i == 10
            Te2 = DD02(t02(3840:end),:);
            DD02(t02(3840:end),:) = [];
            Te4 = DD04(t04(3641:end),:);
            DD04(t04(3641:end),:) = [];
        end 
        if i < 3
            Te3 = DD03(t03((i-1)*1+1:i*1),:);
            DD03(t03((i-1)*1+1:i*1),:) = [];
        else
            Te3 = [];
        end          
        if i <= 2
            Te5 = DD05(t05((i-1)*32+1:i*32),:);
            DD05(t05((i-1)*32+1:i*32),:) = [];
        end
        if i > 2 && i < 10
            Te5 = DD05(t05(65+(i-3)*33+1:65+(i-2)*33),:);
            DD05(t05(65+(i-3)*33+1:65+(i-2)*33),:) = [];
        end
        if i == 10
            Te5 = DD05(t05(296:end),:);
            DD05(t05(296:end),:) = [];
        end               
        Tr1 = DD01;
        Tr2 = DD02;
        Tr3 = DD03;
        Tr4 = DD04;
        Tr5 = DD05;
        TrainData = [Tr1;Tr2;Tr3;Tr4;Tr5];
        TestData = [Te1;Te2;Te3;Te4;Te5];         
        %}
        %{
        %segment--14
        DD1 = D1;
        DD2 = D2;
        DD3 = D3;
        DD4 = D4;
        DD5 = D5;
        DD6 = D6;
        DD7 = D7;
        Te1 = DD1(t01((i-1)*33+1:i*33),:);
        Te2 = DD2(t02((i-1)*33+1:i*33),:);
        Te3 = DD3(t03((i-1)*33+1:i*33),:);
        Te4 = DD4(t04((i-1)*33+1:i*33),:);
        Te5 = DD5(t05((i-1)*33+1:i*33),:);
        Te6 = DD6(t06((i-1)*33+1:i*33),:);
        Te7 = DD7(t07((i-1)*33+1:i*33),:);
        DD1(t01((i-1)*33+1:i*33),:) = [];
        DD2(t02((i-1)*33+1:i*33),:) = [];
        DD3(t03((i-1)*33+1:i*33),:) = [];
        DD4(t04((i-1)*33+1:i*33),:) = [];
        DD5(t05((i-1)*33+1:i*33),:) = [];
        DD6(t06((i-1)*33+1:i*33),:) = [];
        DD7(t07((i-1)*33+1:i*33),:) = [];
        Tr1 = DD1;
        Tr2 = DD2;
        Tr3 = DD3;
        Tr4 = DD4;
        Tr5 = DD5;
        Tr6 = DD6;
        Tr7 = DD7;        
        TrainData = [Tr1;Tr2;Tr3;Tr4;Tr5;Tr6;Tr7];
        TestData = [Te1;Te2;Te3;Te4;Te5;Te6;Te7];             
        %}
        %{
        %Sonar--15
        DD1 = D1;
        DD2 = D2;
        if i <= 3
            Te1 = DD1(t01((i-1)*9+1:i*9),:);
            DD1(t01((i-1)*9+1:i*9),:) = [];
        end
        if i > 3 && i < 10
            Te1 = DD1(t01(27+(i-4)*10+1:27+(i-3)*10),:);
            DD1(t01(27+(i-4)*10+1:27+(i-3)*10),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(88:end),:);
            DD1(t01(88:end),:) = [];
        end                    
        if i <= 9
            Te2 = DD2(t02((i-1)*11+1:i*11),:);
            DD2(t02((i-1)*11+1:i*11),:) = [];
        else
            Te2 = DD2(t02(100:end),:);
            DD2(t02(100:end),:) = [];
        end         
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];         
        %}
        %{
        %Spambase--16
        DD1 = D1;
        DD2 = D2;
        if i <= 3
            Te1 = DD1(t01((i-1)*182+1:i*182),:);
            DD1(t01((i-1)*182+1:i*182),:) = [];
        end
        if i > 3 && i < 10
            Te1 = DD1(t01(546+(i-4)*181+1:546+(i-3)*181),:);
            DD1(t01(546+(i-4)*181+1:546+(i-3)*181),:) = [];
        end
        if i == 10
            Te1 = DD1(t01(1633:end),:);
            DD1(t01(1633:end),:) = [];
        end                    
        if i <= 2
            Te2 = DD2(t02((i-1)*278+1:i*278),:);
            DD2(t02((i-1)*278+1:i*278),:) = [];
        end
        if i > 2 && i < 10
            Te2 = DD2(t02(556+(i-3)*279+1:556+(i-2)*279),:);
            DD2(t02(556+(i-3)*279+1:556+(i-2)*279),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(2510:end),:);
            DD2(t02(2510:end),:) = [];
        end 
        Tr1 = DD1;
        Tr2 = DD2;
        TrainData = [Tr1;Tr2];
        TestData = [Te1;Te2];        
        %}
        %{
        %Vehicle--17 
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        DD04 = D04;        
        if i <= 2
            Te01 = DD01(t01((i-1)*21+1:i*21),:);
            DD01(t01((i-1)*21+1:i*21),:) = [];
            Te02 = DD02(t02((i-1)*22+1:i*22),:);
            DD02(t02((i-1)*22+1:i*22),:) = [];
        end
        if i > 2 && i < 10
            Te01 = DD01(t01(42+(i-3)*22+1:42+(i-2)*22),:);
            DD01(t01(42+(i-3)*22+1:42+(i-2)*22),:) = [];
            Te02 = DD02(t02(44+(i-3)*21+1:44+(i-2)*21),:);
            DD02(t02(44+(i-3)*21+1:44+(i-2)*21),:) = [];
        end
        if i == 10
            Te01 = DD01(t01(197:end),:);
            DD01(t01(197:end),:) = [];
            Te02 = DD02(t02(192:end),:);
            DD02(t02(192:end),:) = [];
        end        
        if i <= 3
            Te03 = DD03(t03((i-1)*21+1:i*21),:);
            DD03(t03((i-1)*21+1:i*21),:) = [];
        end
        if i > 3 && i < 10
            Te03 = DD03(t03(63+(i-4)*22+1:63+(i-3)*22),:);
            DD03(t03(63+(i-4)*22+1:63+(i-3)*22),:) = [];
        end
        if i == 10
            Te03 = DD03(t03(196:end),:);
            DD03(t03(196:end),:) = [];
        end         
        if i < 10
            Te04 = DD04(t04((i-1)*20+1:i*20),:);
            DD04(t04((i-1)*20+1:i*20),:) = [];
        else
            Te04 = DD04(t04(181:end),:);
            DD04(t04(181:end),:) = [];
        end
        Tr01 = DD01;
        Tr02 = DD02;
        Tr03 = DD03;
        Tr04 = DD04;        
        TrainData = [Tr01;Tr02;Tr03;Tr04];
        TestData =  [Te01;Te02;Te03;Te04];        
        %}
        %{
        %Vowel--18
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        DD04 = D04;
        DD05 = D05;
        DD06 = D06;
        DD07 = D07;
        DD08 = D08;
        DD09 = D09;
        DD10 = D10;
        DD11 = D11;        
        Te01 = DD01(t01((i-1)*9+1:i*9),:);
        Te02 = DD02(t02((i-1)*9+1:i*9),:);
        Te03 = DD03(t03((i-1)*9+1:i*9),:);
        Te04 = DD04(t04((i-1)*9+1:i*9),:);
        Te05 = DD05(t05((i-1)*9+1:i*9),:);
        Te06 = DD06(t06((i-1)*9+1:i*9),:);
        Te07 = DD07(t07((i-1)*9+1:i*9),:);
        Te08 = DD08(t08((i-1)*9+1:i*9),:);
        Te09 = DD09(t09((i-1)*9+1:i*9),:);
        Te10 = DD10(t10((i-1)*9+1:i*9),:);
        Te11 = DD11(t11((i-1)*9+1:i*9),:);        
        DD01(t01((i-1)*9+1:i*9),:) = [];
        DD02(t02((i-1)*9+1:i*9),:) = [];
        DD03(t03((i-1)*9+1:i*9),:) = [];
        DD04(t04((i-1)*9+1:i*9),:) = [];
        DD05(t05((i-1)*9+1:i*9),:) = [];
        DD06(t06((i-1)*9+1:i*9),:) = [];
        DD07(t07((i-1)*9+1:i*9),:) = [];
        DD08(t08((i-1)*9+1:i*9),:) = [];
        DD09(t09((i-1)*9+1:i*9),:) = [];
        DD10(t10((i-1)*9+1:i*9),:) = [];
        DD11(t11((i-1)*9+1:i*9),:) = [];          
        Tr01 = DD01;
        Tr02 = DD02;
        Tr03 = DD03;
        Tr04 = DD04;
        Tr05 = DD05;
        Tr06 = DD06;
        Tr07 = DD07;
        Tr08 = DD08;
        Tr09 = DD09; 
        Tr10 = DD10;
        Tr11 = DD11;
        TrainData = [Tr01;Tr02;Tr03;Tr04;Tr05;Tr06;Tr07;Tr08;Tr09;Tr10;Tr11];
        TestData =  [Te01;Te02;Te03;Te04;Te05;Te06;Te07;Te08;Te09;Te10;Te11];           
        %}
        %{
        %waveform--19 
        DD01 = D01;
        DD02 = D02;
        DD03 = D03;
        if i <= 2
            Te1 = DD01(t01((i-1)*170+1:i*170),:);
            DD01(t01((i-1)*170+1:i*170),:) = [];
        end
        if i > 2 && i < 10
            Te1 = DD01(t01(340+(i-3)*169+1:340+(i-2)*169),:);
            DD01(t01(340+(i-3)*169+1:340+(i-2)*169),:) = [];
        end
        if i == 10
            Te1 = DD01(t01(1524:end),:);
            DD01(t01(1524:end),:) = [];
        end         
        if i <= 3
            Te2 = DD02(t02((i-1)*166+1:i*166),:);
            DD02(t02((i-1)*166+1:i*166),:) = [];
        end
        if i > 3 && i < 10
            Te2 = DD02(t02(498+(i-4)*165+1:498+(i-3)*165),:);
            DD02(t02(498+(i-4)*165+1:498+(i-3)*165),:) = [];
        end
        if i == 10
            Te2 = DD02(t02(1489:end),:);
            DD02(t02(1489:end),:) = [];
        end                    
        if i <= 5
            Te3 = DD03(t03((i-1)*165+1:i*165),:);
            DD03(t03((i-1)*165+1:i*165),:) = [];
        end
        if i > 5 && i < 10
            Te3 = DD03(t03(825+(i-6)*166+1:825+(i-5)*166),:);
            DD03(t03(825+(i-6)*166+1:825+(i-5)*166),:) = [];
        end
        if i == 10
            Te3 = DD03(t03(1490:end),:);
            DD03(t03(1490:end),:) = [];
        end  
        Tr1 = DD01;
        Tr2 = DD02;
        Tr3 = DD03;
        TrainData = [Tr1;Tr2;Tr3];
        TestData = [Te1;Te2;Te3];        
        %}
        %{
        %wine--20 
        DD1 = D1;
        DD2 = D2;
        DD3 = D3;
        if i < 10
            Te1 = DD1(t01((i-1)*6+1:i*6),:);
            DD1(t01((i-1)*6+1:i*6),:) = [];
        else
            Te1 = DD1(t01(55:end),:);
            DD1(t01(55:end),:) = [];
        end
        if i <= 8
            Te2 = DD2(t02((i-1)*7+1:i*7),:);
            DD2(t02((i-1)*7+1:i*7),:) = [];
        end
        if i > 8 && i < 10
            Te2 = DD2(t02(56+(i-9)*8+1:56+(i-8)*8),:);
            DD2(t02(56+(i-9)*8+1:56+(i-8)*8),:) = [];
        end
        if i == 10
            Te2 = DD2(t02(65:end),:);
            DD2(t02(65:end),:) = [];
        end
        if i <= 3
            Te3 = DD3(t03((i-1)*4+1:i*4),:);
            DD3(t03((i-1)*4+1:i*4),:) = [];
        end
        if i > 3 && i < 10
            Te3 = DD3(t03(12+(i-4)*5+1:12+(i-3)*5),:);
            DD3(t03(12+(i-4)*5+1:12+(i-3)*5),:) = [];
        end
        if i == 10
            Te3 = DD3(t03(43:end),:);
            DD3(t03(43:end),:) = [];
        end
        Tr1 = DD1;
        Tr2 = DD2;
        Tr3 = DD3;
        TrainData = [Tr1;Tr2;Tr3];
        TestData = [Te1;Te2;Te3];
        %}
        
%-------Train and Test-----------------------------------------------------
        di = size(TrainData,2) - 2;
        if di > 3
            for ki = 1 : di-2
                TrainSets{ki} = TrainData(:,[ki,ki+1,ki+2,di+1,di+2]);
                TestSets{ki} = TestData(:,[ki,ki+1,ki+2,di+1,di+2]);
            end
            TrainSets{ki+1} = TrainData(:,[1,di-1,di,di+1,di+2]);
            TestSets{ki+1} = TestData(:,[1,di-1,di,di+1,di+2]);
            TrainSets{ki+2} = TrainData(:,[1,2,di,di+1,di+2]);
            TestSets{ki+2} = TestData(:,[1,2,di,di+1,di+2]);
            for ki = 1 : di
                [CellSets{ki} CeSaSets{ki}] = InitG(TrainSets{ki},mingen,t);
            end
            tt = size(TestSets{1},1);
            for ii = 1 : tt
                for ki = 1 : di
                    [JGI(ki,:)] = JTestfun(CellSets{ki},CeSaSets{ki},TestSets{ki}(ii,:));
                end
                [Temp1 Temp2] = max(sum(JGI));
                JG = [JG; Temp2-TestSets{ki}(ii,end)];
            end
        else
            [Cells CeSas] = InitG(TrainData,mingen,t);
            tt = size(TestData,1);
            for ii = 1 : tt
                [W] = JTestfun(Cells,CeSas,TestData(ii,:));
                [Temp1,Temp2] = max(W);
                JG = [JG;Temp2-TestData(ii,end)];
            end
        end      
    end

    dgs = find(JG == 0);
    dr(j) = size(dgs,1) / n * 100;
end
Classification_accuracy_10cross = mean(dr)
Standard_deviation_10cross = std(dr',0,1) 



%%
%For low-dimension datasets
%Test stadge of continual learning
function [Wa] = JTestfun(Grids,Grs,data)
%Data:n-dimension and m types
%data:[d1,d2,...,dn,time,label],time is the data acquisition
%Grids:[d1,d2,...,dn,p,q1,t1,q2,t2,...,qm,tm]
%di:dimension
%qi:number of samples in one cell
%ti:the last active time

di = size(data,2) - 2;
t = (size(Grids,2) - di - 1) / 2;
flag = 1;
ii = 1;

while flag
    [A] = IsSampleinGrid(Grids(ii,:),data);
    if ~isempty(A)
        mindj = max(Grids(:,di+1));
        if Grids(ii,di+1) < mindj  %High passage cell
            [TGrids,TGrs] = SuboneG(Grids(ii,:),data,mindj-1);
            ti = 1;
            flagT = 1;
            while flagT
                if ~isempty(TGrs{ti})
                    At = ti;
                    flagT = 0;
                else
                    ti = ti + 1;
                end
            end
            [adwgxh] = Fxlwgfun(TGrids(At,:),Grids,di);  
            TE1 = Grids(adwgxh(:,1),di+2:2:di+2*t);
            dis = dist(Grids(adwgxh(:,1),1:di),data(1:di)');
            disi = TE1 ./ dis;
            if size(adwgxh,1) == 1
                Wa = disi;
            else
                Wa = sum(disi);
            end
            Wa = Wa ./ max(Wa);
        else
            [adwgxh] = Fxlwgfun(Grids(ii,:),Grids,di);
            if sum(Grids(ii,di+2:2:di+2*t)) == 0
                XH = adwgxh;
            else
                XH = [adwgxh;ii];
            end
            TE1 = Grids(XH(:,1),di+2:2:di+2*t);
            dis = dist(Grids(XH(:,1),1:di),data(1:di)');
            disi = TE1 ./ dis;
            Wa = sum(disi); 
            Wa = Wa ./ max(Wa);
        end
        flag = 0;
    end
    ii = ii + 1;
end
end


%%
%To find the adjacent memory cells 
function [xlwg] = Fxlwgfun(zx,Grids,di)
%Data:n-dimension and m types
%data:[d1,d2,...,dn,time,label],time is the data acquisition
%Grids:[d1,d2,...,dn,p,q1,t1,q2,t2,...,qm,tm]
%di:dimension
%qi:number of samples in one cell
%ti:the last active time

t = (size(Grids,2) - di - 1) / 2;
dqdj = zx(di+1);
dltyd = 0.5 / 2^dqdj;
TE1 = Grids(:,di+2:2:di+2*t)';
TE2 = find(TE1 < 0);
TE1(TE2(:)) = 0;
NTS = sum(TE1)';
AA = find(Grids(:,di+1) <= dqdj & NTS(:,1) > 0);
BB = Grids(AA(:,1),:);
tt = size(BB,1);
Gdzx = zx .* ones(tt,di+2*t+1);
Gdyd = 0.5 ./ 2.^BB(:,di+1);

fi = 0;
flag = 1;
while flag
    dltyd =(fi + 0.5) / 2^dqdj;
    zxyd = dltyd .* ones(tt,1);
    
    Gdab = abs(abs(Gdzx(:,1:di) - BB(:,1:di)) - Gdyd - zxyd);
    Gdmin = min(Gdab')';
    A = find(Gdmin <= 0.001 | Gdmin == 0);
    Grd = BB(A(:,1),:);
    Dis = dist(Grd(:,1:di),zx(1,1:di)');
    Diss = Dis - (di .* Gdyd(A(:,1),:) .* Gdyd(A(:,1),:)) .^ 0.5;
    dt = (di * dltyd * dltyd) ^ 0.5;
    B = find(abs(Diss - dltyd) <= 0.0001 | abs(Diss - dt) <= 0.0001 | Diss <= dt);
    
    if ~isempty(B)
        flag = 0;
    else
        fi = fi + 1;
    end 
end
%Return the sequence number of cell
xlwg = AA(A(B(:,1),:),:);
end

%%
%Divide the memory cell which has one sample to the smallest memory cell
function [TGrids,TGrs] = SuboneG(grid,data,xfdj)
%Data:n-dimension and m types
%data:[d1,d2,...,dn,time,label],time is the data acquisition
%Grids:[d1,d2,...,dn,p,q1,t1,q2,t2,...,qm,tm]
%di:dimension
%qi:number of samples in one cell
%ti:the last active time

di = size(data,2) - 2;
t = (size(grid,2) - di - 1) / 2;
dqdj = grid(di+1);
Temp = [];
Tempg = [];
TGrids = [];
TGrs = [];
if dqdj < xfdj
    dltyd = 1 / 2^(dqdj+1);
    yd = 0.5 * dltyd;
    for i = 1 : di
        k = 2^(i-1);
        Tei = [-yd.*ones(k,1); yd .* ones(k,1)];
        n = size(Tei,1);
        m = 2^di/n;
        Te = [];
        for j = 1 : m
            Te = [Te;Tei];
        end
        Temp(:,i) = grid(i) + Te;
    end    
    Temp = [Temp (dqdj+1).*ones(2^di,1) zeros(2^di,2*t)];  
    for i = 1 : 2^di
        Tempg{i,1} = [];
    end
    flagt = 1;
    while flagt && j <= 2^di
        [A] = IsSampleinGrid(Temp(j,:),data);
        if ~isempty(A)
            Tempg{j} = data;
            %Temp(j,di+(2*data(end))) = 1;
            %Temp(j,di+1+(2*data(end))) = data(end-1);
            [TGrids,TGrs] = SuboneG(Temp(j,:),data,xfdj);
            Temp(j,di+1) = 999999;
            flagt = 0;
        else
            j = j + 1;
        end
    end
    TGrids = [TGrids;Temp];
    TGrs = [TGrs;Tempg];
else
    dltyd = 1 / 2^(dqdj+1);
    yd = 0.5 * dltyd;
    for i = 1 : di
        k = 2^(i-1);
        Tei = [-yd.*ones(k,1); yd.*ones(k,1)];
        n = size(Tei,1);
        m = 2^di/n;
        Te = [];
        for j = 1 : m
            Te = [Te;Tei];
        end
        Temp(:,i) = grid(i) + Te;
    end    
    Temp = [Temp (dqdj+1).*ones(2^di,1) zeros(2^di,2*t)];   
    for i = 1 : 2^di
        Tempg{i,1} = [];
    end
    j = 1;
    flagt = 1;
    while flagt && j <= 2^di
        [A] = IsSampleinGrid(Temp(j,:),data);
        if ~isempty(A)
            Tempg{j} = data;
            %Temp(j,di+(2*data(end))) = 1;
            %Temp(j,di+1+(2*data(end))) = data(end-1);
            flagt = 0;
        else
            j = j + 1;
        end
    end
    TGrids = [TGrids;Temp];
    TGrs = [TGrs;Tempg];
end
AA = find(TGrids(:,di+1) == 999999);
if ~isempty(AA)
    TGrids(AA(:,1),:) = [];
    TGrs(AA(:,1),:) = [];
end
end


%%
%initializing memory cells
function [Grids,Grs] = InitG(Data,mingen,t)
%Grs records the samples in every memory cell, it is used to check the result of Grids and continual learning
%Data:n-dimension and m types
%data:[d1,d2,...,dn,time,label]
%Grids:[d1,d2,...,dn,p,q1,t1,q2,t2,...,qm,tm]
%di:dimension
%p:passage
%q:number of samples in one cell;qi type i
%t:the last active time
di = size(Data,2) - 2;                                                     %The dimensionality of Data

GC = 0.5 .* ones(1,di);
%1-di,center of grid; di+1,passage, di+2,sample size of type1, di+3,sample size of type2
Grids(1,:) = [GC zeros(1,2*t+1)];                                            %The initial center, passage, number of type1, number of type2
Grs{1,1} = [];

%Adaptive to generate memory cells
[Grids,Grs] = AdjustG(Grids,Grs,0,Data,mingen);                            %Adaptive to generate memory cells
%DrawGrid(Grids,1)
end


%%
%Adjusting memory cells and samples
function [Grids,Grs] = AdjustG(Grids,Grs,gdj,Data,mingen)
%gdj is the passage of memory cell for adjusting
%Data:n-dimension and m types
%data:[d1,d2,...,dn,label]
%Grids:[d1,d2,...,dn,p,t1,t2,...,tm]
%di:dimension
%t:types
tt = size(Grids,1);
di = size(Data,2) - 2;
for ii = 1:tt
    [A] = IsSampleinGrid(Grids(ii,:),Data);
    Grs{ii} = A;   
    if ~isempty(A)
        pas = Grids(ii,di+1);
        N = unique(A(:,end));
        if pas < mingen            
            [TempG,TempS] = Xfwgfun(Grids(ii,:),gdj,Grs{ii},mingen);              %Divede memory cell
            Grids(ii,di+1) = 999999;
            Grids = [Grids;TempG];
            Grs = [Grs;TempS];
        else
            tj = size(N,1);
            for jj = 1 : tj
                TA = find(A(:,end) == N(jj));
                Grids(ii,di+2*N(jj)) = size(TA,1);
                Grids(ii,di+2*N(jj)+1) = A(TA(1),end-1);
            end
        end
    end
end
AA = find(Grids(:,di+1) == 999999);
if ~isempty(AA)
    Grids(AA(:,1),:) = [];
    Grs(AA(:,1),:) = [];
end
end


%%
%The sample in or not in a memory cell
function [data] = IsSampleinGrid(grid,Data)
%Data:n-dimension and m types
%data:[d1,d2,...,dn,label]
%Grids:[d1,d2,...,dn,p,t1,t2,...,tm]
%di:dimension
%t:types
di = size(Data,2) - 2;
dltyd = 1 / 2^grid(di+1);
flag = 1;
i = 1;
while flag && i <= di
    L = grid(i) - 0.5 * dltyd;
    R = grid(i) + 0.5 * dltyd;
    A = find(Data(:,i) <= R & Data(:,i) >= L);
    if isempty(A)
        flag = 0;
    else
        Data = Data(A(:,1),:);
    end
    i = i + 1;
end

if flag
    data = Data;
else
    data = [];
end

end


%%
%Divide memory cell
function [Grids,Grs]= Xfwgfun(grid,xfdj,data,mingen)
%Data:n-dimension and m types
%data:[d1,d2,...,dn,label]
%Grids:[d1,d2,...,dn,p,t1,t2,...,tm]
%di:dimension
%t:types
dltyd = 1 / 2^(xfdj+1);                                                    %The length of other divide memory cell
yd = 0.5 * dltyd;
di = size(data,2) - 2;
t = (size(grid,2) - di - 1) / 2;
for i = 1 : di
    k = 2^(i-1);
    Tei = [-yd.*ones(k,1); yd.*ones(k,1)];
    n = size(Tei,1);
    m = 2^di/n;
    Te = [];
    for j = 1 : m
        Te = [Te;Tei];
    end
    Temp(:,i) = grid(i) + Te;
end
Temp = [Temp (xfdj+1).*ones(2^di,1) zeros(2^di,2*t)];
for i = 1 : 2^di
    Grs{i,1} = [];
end
[Grids,Grs] = AdjustG(Temp,Grs,xfdj+1,data,mingen);
end


%%
%Drawing memory cell in [0,1]^2
function DrawGrid(Grids,k)
%k is the figure number
[tt1 tt2] = size(Grids);
tt = tt2 - 3;
kt = ceil(tt / 6);
cl = 1/(kt+1);
mt = max(Grids(:,end));

figure(k)
hold on
for fi = 1 : tt1
    dltyd = 1 / 2^Grids(fi,3);
    L = Grids(fi,1) - 0.5 * dltyd;
    R = Grids(fi,1) + 0.5 * dltyd;
    U = Grids(fi,2) + 0.5 * dltyd;
    B = Grids(fi,2) - 0.5 * dltyd;
    for kk = 1 : 2 : tt
        TE1 = Grids(fi,3+1:2:3+tt);
        TE2 = find(TE1>0);
        TET = sum(TE1(TE2(:)));
        if Grids(fi,3+kk) > 0 && Grids(fi,3+kk) == TET %one type
            cc = [0 0 0];
            ft = floor(kk/2) + 1;
            cc(mod(ft-1,3)+1) = cl * (floor((ft-1)/3)+1) * Grids(fi,3+kk+1)/mt;
            X = [L L R R];
            Y = [B U U B];
            fill(X,Y,cc)
        end
        if Grids(fi,3+kk) > 0 && Grids(fi,3+kk) < TET %more than one type
            cc = [0.25 0.5 0.75];
            X = [L L R R];
            Y = [B U U B];
            fill(X,Y,cc)
        end        
    end
    plot([L;L;R;R;L],[B;U;U;B;B],'g-');
end
plot([0,1],[1,1],'k-');
plot([0,1],[0,0],'k-');
plot([0,0],[1,0],'k-');
plot([1,1],[1,0],'k-');
axis equal;
axis off
set(gcf,'color','w')
end


%%
%normalize data to [0,1]n
function [Dn] = norma(D)
if max(D) == 0
    Dn = zeros(size(D,1),1);
else
    Dn = (D - min(D)) ./ (max(D) - min(D));
end
end


%%
%normalize data to [0.0001,0.9999]n
function [Dn] = normat(D)
if max(D) == 0
    Dn = zeros(size(D,1),1);
else
    Dn = 0.0001+0.9998.*(D - min(D)) ./ (max(D) - min(D));
end
end